package com.ruoyi.mall.service;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.MallGoodsNorm;
import com.ruoyi.mall.mapper.MallGoodsNormMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallGoodsMapper;
import com.ruoyi.mall.domain.MallGoods;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Service
public class MallGoodsService {
    @Autowired
    private MallGoodsMapper mallGoodsMapper;
    @Autowired
    private MallGoodsNormMapper mallGoodsNormMapper;

    /**
     * 查询商品
     *
     * @param goodsId 商品主键
     * @return 商品
     */
    public MallGoods selectMallGoodsByGoodsId(Long goodsId) {
        return mallGoodsMapper.selectMallGoodsByGoodsId(goodsId);
    }

    /**
     * 查询商品列表
     *
     * @param mallGoods 商品
     * @return 商品
     */
    public List<MallGoods> selectMallGoodsList(MallGoods mallGoods) {
        return mallGoodsMapper.selectMallGoodsList(mallGoods);
    }

    /**
     * 新增商品
     *
     * @param mallGoods 商品
     * @return 结果
     */
    public int insertMallGoods(MallGoods mallGoods) {
        mallGoods.setCreateTime(DateUtils.getNowDate());
        // 添加商品
        mallGoods.setGoodsNo(createGoodsNo());
        mallGoods.setDelFlag("0");
        mallGoods.setStatus(1);
        return mallGoodsMapper.insertMallGoods(mallGoods);
    }

    /**
     * 修改商品
     *
     * @param mallGoods 商品
     * @return 结果
     */
    public int updateMallGoods(MallGoods mallGoods) {
        if (mallGoods.getStatus() == 0) {
            // 启用，必须设置好规格
            MallGoodsNorm queryNormParam = new MallGoodsNorm();
            queryNormParam.setGoodsId(mallGoods.getGoodsId());
            List<MallGoodsNorm> normList = mallGoodsNormMapper.selectMallGoodsNormList(queryNormParam);
            if (CollectionUtils.isEmpty(normList)){
                throw new ServiceException("启用商品必须设置好规格");
            }
        }
        mallGoods.setUpdateTime(DateUtils.getNowDate());
        return mallGoodsMapper.updateMallGoods(mallGoods);
    }

    /**
     * 批量删除商品
     *
     * @param goodsIds 需要删除的商品主键
     * @return 结果
     */
    public int deleteMallGoodsByGoodsIds(Long[] goodsIds) {
        return mallGoodsMapper.deleteMallGoodsByGoodsIds(goodsIds);
    }

    /**
     * 删除商品信息
     *
     * @param goodsId 商品主键
     * @return 结果
     */
    public int deleteMallGoodsByGoodsId(Long goodsId) {
        return mallGoodsMapper.deleteMallGoodsByGoodsId(goodsId);
    }

    public String createGoodsNo() {
        String date = DateUtils.dateTime();
        String maxNo = mallGoodsMapper.selectMaxNo(date);
        String goodsNo = date;
        if (StringUtils.isBlank(maxNo)) {
            goodsNo += "0001";
        } else {
            String suffix = maxNo.substring(maxNo.length() - 4);
            suffix = String.format("%04d", Integer.parseInt(suffix) + 1);
            goodsNo += suffix;
        }
        return goodsNo;
    }
}
